package com.yonyou.brigade.cron.user.rtpnr;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.rtpnr.service.IRtpnrUserService;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.ybzboot.starter.elasticjob.lite.annotation.ElasticJobConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/6/13.
 */
//@ElasticJobConfig(cron = "0 25 1 * * ?", shardingTotalCount = 1, shardingItemParameters = "0=Beijing,1=Shanghai,2=Guangzhou")
//@Component
public class SyncRtpnrUserJob implements SimpleJob {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    @Qualifier("rtpnrUserServiceImpl")
    private IRtpnrUserService rtpnrUserService;

    @Autowired
    @Qualifier("briChannelServiceImpl")
    private IBriChannelService briChannelService;

    @Override
    public void execute(ShardingContext shardingContext) {
        List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel("rtpnr");
        if (briChannels != null && briChannels.size() > 0) {
            for (int i = 0; i < briChannels.size(); i++) {
                BriChannel briChannel = briChannels.get(i);
//                if (briChannel != null && BriChannel.NEEDSYNCUSER == briChannel.getNeedSyncUser()) {
                    String tenant = briChannel.getTenant();
                    String orgpk = briChannel.getOrgpk();
                    try {
                        Map result = rtpnrUserService.syncRtpnrUsers(tenant, orgpk);
                        if (!result.get("code").equals("0")) {
                            logger.error(result.get("information").toString());
                        }
                    } catch (Exception e) {
                        logger.error(e.getMessage());
                    }
//                }
            }
        }
    }
}
